<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Queue\Capsule\Manager;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\CreateStateConfigRequest;
use App\Http\Requests\UpdateStateConfigRequest;
use App\StateConfig;
use Illuminate\Support\Facades\Auth;

class StateConfigController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index() {
		$stateConfigObj = StateConfig::all();
//		dd($stateConfigObj);

		$data['stateConfigs'] = $stateConfigObj;

		return view('stateConfig.list')->with($data);
	}

    public function create()
    {
        return view('stateConfig.create');
    }


    public function store(CreateStateConfigRequest $request){
        $stateConfigObj = new StateConfig();
//	    $stateConfigObj->modal =$request->model;
	    $stateConfigObj->keywords =$request->keywords;
	    $stateConfigObj->state =$request->state;
	    $stateConfigObj->stateName =$request->stateName;
	    $stateConfigObj->remark =$request->remark;

        if($stateConfigObj->save()){

	        $adminUser = Auth::user();
	        if($adminUser){
		        $userId = $adminUser->id;
		        $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').' 新增了状态：【  '.$request->stateConfig_name.' 】';
		        adminOperationLog($request,$userId,$logs);
	        }

            return redirect('stateConfig/list')->with('success','新增成功');
        }else{
            return redirect()->back()->withInput();
        }
    }


    public function edit($id)
    {
	    $stateConfigObj = StateConfig::find($id);

        $data['stateConfigInfo'] = $stateConfigObj;
//        dd($stateConfigObj);

        return view('stateConfig.edit')->with($data);

    }


    public function update(UpdateStateConfigRequest $request, $id){

	    $stateConfigObj = stateConfig::find($id);
	    $stateConfigObj->keywords =$request->keywords;
	    $stateConfigObj->state =$request->state;
	    $stateConfigObj->stateName =$request->stateName;
	    $stateConfigObj->remark =$request->remark;

        if(!$stateConfigObj->save()){
            return Redirect::back()->withInput()->withErrors(['fail' => trans('message.updateFail')]);
        }

	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'编辑了状态：【 '.$request->keywords.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return Redirect::to(url('stateConfig/list'))->with('success', trans('message.updateSuccess'));

    }


}





